package logic

import (
	"autotestservice/testservices/internal/deviceMng"
	"context"
	"gitee.com/jmdvirus/jkgo/jk/jklog"

	"autotestservice/testservices/internal/svc"
	"autotestservice/testservices/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type AutotestDeviceStatusLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAutotestDeviceStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) AutotestDeviceStatusLogic {
	return AutotestDeviceStatusLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AutotestDeviceStatusLogic) AutotestDeviceStatus(req types.TestDevice) (*types.Response, error) {
	jklog.L().Debugln("device status ip=[%s]", req.Addr)
	res := &types.Response{
		Code:    0,
		Message: "",
	}
	devOpt := deviceMng.NewDeviceOption(req.ConnectStyle, req.Addr, req.Port)
	findDev := deviceMng.GlobalDeviceMng().Find(*devOpt)
	if findDev == nil {
		res.Code = 10
		res.Message = "disconnected"
	} else {
		res.Message = "connected"
	}
	return res, nil
}
